package com.aw.guiguzi.module.manager._user

import android.view.View
import android.widget.EditText
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.widgets.AwViewCustomToolbar.OnRightClickListener
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.constants.MyConstant
import com.aw.guiguzi.databinding.ActivityManagerUserEditBinding
import com.aw.guiguzi.event.EventRefreshUserDetailType
import com.aw.guiguzi.event.EventRefreshUserListType
import com.aw.guiguzi.module.manager._user.presenters.UserEditPresent
import com.aw.guiguzi.module.manager._user.views.UserEditView

class UserEditActivity : AwMvpBindingActivity<UserEditPresent?, ActivityManagerUserEditBinding?>(),
    UserEditView.View, OnRightClickListener {
    private var mBean: UserInfoBean? = null
    private var pageTag = 0
    private var originTxt: String? = ""
    override fun createPresenter(): UserEditPresent {
        return UserEditPresent(this)
    }

    override fun getViewBinding(): ActivityManagerUserEditBinding {
        return ActivityManagerUserEditBinding.inflate(layoutInflater)
    }

    override fun initData() {
        super.initData()
        mBean = intent.getSerializableExtra(Extras.params) as UserInfoBean?
        if (mBean == null) {
            showDialogToFinish("数据异常")
            return
        }
        pageTag = intent.getIntExtra(Extras.params2, -1)
        when (pageTag) {
            MyConstant.USER_EDIT_TAG_NICKNAME -> {
                originTxt = mBean!!.nickname
                setText(binding?.etContent, mBean!!.nickname)
                setPageToolBar("修改昵称")
            }
        }
    }

    override fun initListener() {
        super.initListener()
        findViewById<View>(R.id.iv_del).setOnClickListener { v: View? ->
            showDialogWithCancelDismiss(
                "确认当前数据吗？"
            ) { v1: View? -> setTextNoDefault(binding?.etContent, "") }
        }
    }

    private fun setPageToolBar(title: String) {
        setToolbarWithBackImgAndRightTextHasColor(
            title,
            "✔",
            com.aw.baselib.R.color.blue_dark,
            this
        )
    }

    override fun onRightTextClick() {
        if (pageTag == -1) {
            showMsg("无效的修改项")
            return
        }
        if (AwDataUtil.isEmpty(getText(binding?.etContent))) {
            showMsg("请输入修改内容")
            return
        }
        if (!AwDataUtil.isEmpty(originTxt) && originTxt == getText(binding?.etContent)) {
            showMsg("内容未作更改，无需提交")
            return
        }
        when (pageTag) {
            MyConstant.USER_EDIT_TAG_NICKNAME -> mBean!!.nickname = getText(binding?.etContent)
        }
        mPresenter!!.updateUserInfo(mBean)
    }

    override fun updateUserInfoSuccess() {
        showMsg("用户信息更新成功")
        postEvent(EventRefreshUserListType(null))
        postEvent(EventRefreshUserDetailType(null))
        finish()
    }
}